<template>

  <div>
    <div class="filter-container">
      <div class="filter-item">
        <span>URI:</span>
        <el-input v-model="listQuery.requestUri" clearable placeholder="请输入URI"/>
      </div>
      <div class="filter-item">
        <span>操作IP:</span>
        <el-input v-model="listQuery.operationIp" clearable placeholder="请输入操作IP"/>
      </div>
      <el-button class="filter-item" type="primary" icon="Search" @click="handleFilter">搜索</el-button>
      <el-button class="filter-item" text type="primary" icon="Delete" @click="clearQuery">清除条件</el-button>
    </div>
    <div class="operationList">
      <div>
        <el-button text type="primary" icon="Delete" @click="handleBatchDelete">批量删除</el-button>
      </div>
      <div>
        <el-button text type="primary" icon="RefreshRight" @click="handleFilter">刷新</el-button>
      </div>
    </div>
    <el-table
      ref="multipleTable"
      v-loading="listLoading"
      :data="list"
      :row-class-name="tableRowClass"
      fit
      border
      highlight-current-row
      header-cell-class-name="header-cell"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection"/>
      <el-table-column label="序号" width="100" type="index" :index="calIndex"/>
      <el-table-column min-width="120" label="操作标题">
        <template #default="scope">
          <span>{{ scope.row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="80" label="操作类型">
        <template #default="scope">
          <span>{{ scope.row.logType }}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="120" label="URI">
        <template #default="scope">
          <span>{{ scope.row.requestUri }}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="80" label="操作人">
        <template #default="scope">
          <span>{{ scope.row.operationName }}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="80" label="操作IP">
        <template #default="scope">
          <span>{{ scope.row.operationIp }}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="80" label="浏览器">
        <template #default="scope">
          <span>{{ scope.row.browser }}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="100" label="操作系统">
        <template #default="scope">
          <span>{{ scope.row.os }}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="80" label="请求状态">
        <template #default="scope">
          <el-tag :type="statusTypeFilter(scope.row.status)">{{ statusFilter(scope.row.status) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column min-width="160" label="操作时间">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="small" type="danger" plain @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0" v-model:page="listQuery.page" v-model:limit="listQuery.limit" :total="total"
      :page-sizes="pageArray" @pagination="getList"/>
  </div>
</template>

<script>
import { deleteOperationLog, fetchOperationLogList } from '@/api/monitor/log/operation'
import Pagination from '@/components/Pagination/index.vue'

export default {
  name: 'Operation',
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      pageArray: this.$store.dictStore.pageArray,
      listQuery: {
        page: 1,
        limit: this.$store.dictStore.defaultPageSize,
        importance: undefined,
        title: undefined,
        type: undefined
      },
      showReviewer: false,
      dialogFormVisible: false,
      multipleSelection: [],
      batchDeleteLoading: false,
      statusOptions: [
        {
          label: '发送失败',
          value: '-1'
        },
        {
          label: '发送中',
          value: '0'
        },
        {
          label: '发送成功',
          value: '1'
        }
      ]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /**
     * 获取状态
     * @param status 状态
     */
    statusTypeFilter(status) {
      const statusMap = {
        0: 'danger',
        1: 'success'
      }
      return statusMap[status]
    },
    /**
     *
     * @param status 状态
     * @returns 状态描述
     */
    statusFilter(status) {
      const statusMap = {
        0: '失败',
        1: '成功'
      }
      return statusMap[status]
    },
    /**
     * 获取列表
     */
    getList(loading = true) {
      this.listLoading = loading
      fetchOperationLogList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
    },
    /**
     * 搜索
     */
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    /**
     * 删除
     * @param row 行数据
     */
    handleDelete(row) {
      this.$confirm('确定删除该数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteOperationLog([row.id]).then((response) => {
          if (response.data.success) {
            row.$delAnim = true
            this.getList()
            this.$message.success(response.data.msg)
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    },
    /**
     * 勾选列表数据
     * @param val 选中数据
     */
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    /**
     * 批量删除
     */
    handleBatchDelete() {
      if (this.multipleSelection.length) {
        this.$confirm('确定删除该数据吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.batchDeleteLoading = true
          const ids = this.multipleSelection.map(item => item.id)
          deleteOperationLog(ids).then((response) => {
            this.batchDeleteLoading = false
            if (response.data.success) {
              this.multipleSelection.forEach(item => {
                item.$delAnim = true
              })
              this.$refs.multipleTable.clearSelection()
              this.$message.success(response.data.msg)
              this.getList(false)
            } else {
              this.$message.error(response.data.msg)
            }
          }).catch(() => {
            this.batchDeleteLoading = false
          })
        })
      } else {
        this.$message({
          message: '至少选择一条日志',
          type: 'warning'
        })
      }
    }
  }
}
</script>
<style scoped lang="scss">

</style>
